/********************************************************************
	Rhapsody	: 7.5 
	Login		: kbe
	Component	: DefaultComponent 
	Configuration 	: CodeConfig
	Model Element	: Client
//!	Generated Date	: Thu, 27, Sep 2012  
	File Path	: DefaultComponent\CodeConfig\Client.cpp
*********************************************************************/

//## auto_generated
#include "Client.h"
//## dependency Connector
#include "Connector.h"
//## link itsReactor
#include "Reactor.h"
//## dependency Patient
#include "Patient.h"
//## auto_generated
#include <oxf\omthread.h>
//## dependency Alarm
#include "Alarm.h"
//## dependency Log
#include "Log.h"
//## package Default

//## class Client
Client::Client(IOxfActive* theActiveContext) {
    setActiveContext(theActiveContext, false);
    itsReactor = NULL;
    initStatechart();
}

Client::~Client() {
    cleanUpRelations();
}

void Client::CreateConnector() {
    //#[ operation CreateConnector()
    #define DEFAULT_PORT_PATIENT	27016
    
    unsigned long address = 0x7F000001;   
    	
    // Test with all event services (Log, Patient and Alarm)
    Connector<Patient, SOCK_Connector> *patientConnector = new Connector<Patient, SOCK_Connector>(itsReactor);
    reqPatient = new Patient(itsReactor);
    INET_Addr *patientAddr = new INET_Addr(DEFAULT_PORT_PATIENT, address);   
    patientConnector->connect(reqPatient, patientAddr, false); 
    //patientConnector->connect(reqPatient, patientAddr, true); 
    
    //#]
}

void Client::Initialize() {
    //#[ operation Initialize()
    	WSADATA wsa_data;
    	WORD version_requested = MAKEWORD(2, 2);
    	int error = WSAStartup(version_requested, &wsa_data);
    	if (error != 0) printf("WSAStartup error \n");
    	itsReactor = Reactor::instance();     
    //#]
}

void Client::SimulateEvents() {
    //#[ operation SimulateEvents()
    char buffer[SIZE_BIG];
    //itsReactor->handleEvents(); 
    printf("Patient CPR > ");
    scanf("%s", buffer);
    reqPatient->getPatientInfo(buffer);
    itsReactor->handleEvents(); 
     
    //#]
}

Reactor* Client::getItsReactor() const {
    return itsReactor;
}

bool Client::startBehavior() {
    bool done = false;
    done = OMReactive::startBehavior();
    return done;
}

void Client::initStatechart() {
    rootState_subState = OMNonState;
    rootState_active = OMNonState;
}

void Client::cleanUpRelations() {
    if(itsReactor != NULL)
        {
            itsReactor = NULL;
        }
}

Patient* Client::getReqPatient() const {
    return reqPatient;
}

void Client::setReqPatient(Patient* p_reqPatient) {
    reqPatient = p_reqPatient;
}

void Client::rootState_entDef() {
    {
        rootState_subState = Idle;
        rootState_active = Idle;
    }
}

IOxfReactive::TakeEventStatus Client::rootState_processEvent() {
    IOxfReactive::TakeEventStatus res = eventNotConsumed;
    switch (rootState_active) {
        case Idle:
        {
            if(IS_EVENT_TYPE_OF(Start_Default_id))
                {
                    //#[ transition 1 
                    Initialize();
                    //#]
                    rootState_subState = Registered;
                    rootState_active = Registered;
                    res = eventConsumed;
                }
            
        }
        break;
        case Registered:
        {
            if(IS_EVENT_TYPE_OF(Connect_Default_id))
                {
                    //#[ transition 2 
                    CreateConnector();
                    //#]
                    rootState_subState = Connected;
                    rootState_active = Connected;
                    res = eventConsumed;
                }
            
        }
        break;
        case Connected:
        {
            if(IS_EVENT_TYPE_OF(Simulate_Default_id))
                {
                    //#[ transition 3 
                    SimulateEvents();
                    //#]
                    rootState_subState = Connected;
                    rootState_active = Connected;
                    res = eventConsumed;
                }
            
        }
        break;
        default:
            break;
    }
    return res;
}

/*********************************************************************
	File Path	: DefaultComponent\CodeConfig\Client.cpp
*********************************************************************/
